// chainmine.txt - This is a mine like mine.txt, with one difference. It 
// only explodes when it receives message 100. This message will usually
// be broadcast from the script minetrigger.txt

// Memory Cells - 
//   0 - Mine severity. Mine does 1-8 times the severity damage to 
//      everyone within 6 spaces. If left at 0, defaults to 4.

//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. 
//     Otherwise, this stuff done flag is set to 1 when the mine is discharged.
//     If the flag is non-zero, then the mine is disarmed and will not go off.
//     Also, if the flag is non-zero when the party enters the zone, the mine 
//     switches to its "exploded" graphic.
//	 4 - Damage type. If left at 0, does non-blockable damage. If 1, does fire
//     damage. If 2, cold damage. If 3, magic damage.

beginterrainscript; 

variables;
	short i_am_active = 1;
	short mine_severity = 4;
	short choice,r1;
body;

beginstate INIT_STATE;
	set_script_mode(2);
	
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_active = 0;
			flip_terrain(my_loc_x(),my_loc_y());
			}
		}

	if (get_memory_cell(0) != 0)
		mine_severity = get_memory_cell(0);
	break;

beginstate START_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_active = 0;
			}
		}


	if (i_am_active == 0)
		end();
		
	if (my_current_message() == 100) {
		r1 = get_ran(mine_severity,1,8);
	
		if (get_memory_cell(4) == 0) { // unblockable damage
			put_boom_on_space(my_loc_x(),my_loc_y(),1,0);
			run_animation_sound(51);
			damage_nearby(r1,6,4,2);
			}
		if (get_memory_cell(4) == 1) { // fire damage
			put_boom_on_space(my_loc_x(),my_loc_y(),1,0);
			run_animation_sound(169);
			damage_nearby(r1,6,1,2);
			}
		if (get_memory_cell(4) == 2) { // cold damage
			put_boom_on_space(my_loc_x(),my_loc_y(),3,0);
			run_animation_sound(75);
			damage_nearby(r1,6,5,2);
			}
		if (get_memory_cell(4) == 3) { // magic damage
			put_boom_on_space(my_loc_x(),my_loc_y(),4,0);
			run_animation_sound(164);
			damage_nearby(r1,6,3,2);
			}
			
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_flag(get_memory_cell(2),get_memory_cell(3)) > 0) {
				set_flag(get_memory_cell(2),get_memory_cell(3),1); 
				}
			}
		flip_terrain(my_loc_x(),my_loc_y());
		i_am_active = 0;
		}
break;
